package com.wsoft.cms.controller;

import com.github.pagehelper.PageInfo;
import com.wsoft.cms.query.announcement.*;
import com.wsoft.cms.service.ICmsAnnouncementService;
import com.wsoft.cms.vo.anno.AnnouncementListVO;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 新闻公告表 前端控制器
 * @author Z001
 * @since 2024年11月27日 10:32:23
 */
@RestController
@Tag(name = "新闻公告")
@RequestMapping("/cms/anno")
public class CmsAnnouncementController {

    @Resource
    private ICmsAnnouncementService announcementService;

    //############################### 管理端 ###############################

    @Operation(summary = "新增新闻公告", description = "新增新闻公告")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid AnnouncementInsertQuery query) {
        boolean b = announcementService.insertAnno(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改新闻公告", description = "修改新闻公告")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid AnnouncementUpdateQuery query) {
        boolean b = announcementService.updateAnno(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改新闻公告附件", description = "修改新闻公告附件")
    @PostMapping("/updateFile")
    public ResponseResult updateFile(@RequestBody @Valid AnnouncementFileUpdateQuery query) {
        boolean b = announcementService.updateFile(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "发布公告", description = "发布公告")
    @PostMapping("/publish")
    public ResponseResult publish(@RequestBody AnnouncementPublishQuery query) {
        return ResponseResult.success(announcementService.publish(query));
    }

    @Operation(summary = "撤销公告", description = "撤销公告")
    @GetMapping("/cancel")
    public ResponseResult cancel(@RequestParam("id") Long id) {
        return ResponseResult.success(announcementService.cancel(id));
    }

    @Operation(summary = "新闻公告详情-后台端", description = "新闻公告详情-后台端")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(announcementService.getInfoByAdmin(id));
    }

    @Operation(summary = "公告分页-后台端", description = "公告分页-后台端")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody AnnouncementListQuery query) {
        PageInfo<AnnouncementListVO> page = announcementService.listAnno(query,false);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "删除公告-后台端", description = "删除公告-后台端")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = announcementService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

}
